import 'package:flutter/material.dart';
import 'reason.dart';
import 'package:dio/adapter.dart';
import 'dart:async';
import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'Util.dart';
import 'package:dio/dio.dart';
class CardPage extends StatefulWidget {
  CardPage({Key key, this.title,this.para}) : super(key: key);
  final String title;
  final Map<String,dynamic> para;
  @override
  _CardPage createState() => _CardPage();
}

class _CardPage extends State<CardPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
  }
  List<Map<String,dynamic>> types = [];
  loadData() {
    SharedPreferences.getInstance().then((value){
      setState(() {
        String domain = value.getString("buildings").toMap()["subdomain"].toString();
        print(domain);
        var dic = widget.para;
        dic["api_key"] = "xQITe6bZFUrmeJbkcKgkUzajUI6FGLA";
        dic["lang"] = "en";
        print(dic);
        var dio = Dio();
        if (ShareHandle.instance.proxy.length > 0){
          (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (HttpClient client){
            client.findProxy = (uri) {
              return ShareHandle.instance.proxy;
            };
          };
        }
        dio.post("${domain}/api/visitor/id_type",data:FormData.fromMap(dic)).then((response){
          final result = response.toResult();
          if (result.item2){
            setState(() {
              this.types = List<Map<String, dynamic>>.from(result.item1["container"] ?? []);
            });
          }else{
            showAlert(result.item4);
          }
        }).catchError((error){
          print("error:${error}");
          showAlert(error.toString());
        });
      });
    });
  }
  var id = "";
  int type = 0;
  @override
  Widget build(BuildContext context) {
    List<Widget> rows = [
      Text("證件類別",style: TextStyle(fontWeight: FontWeight.bold)),
      SizedBox(
        width: 30,
      ),];
    for(final t in this.types){
      rows.addAll([Radio(
          value: int.parse(t["id"]),
          groupValue: this.type,
          onChanged: (str) {
            setState(() {
              type = int.parse(t["id"]);
            });
          }),
        Text(t["name"],style: TextStyle(fontWeight: FontWeight.bold)),
        SizedBox(
          width: 30,
        )]);
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(padding: EdgeInsets.all(20),child: Column(
        children: <Widget>[
          Text("請輸入證件資料",style: TextStyle(fontWeight: FontWeight.bold,fontSize: 21)),
          SizedBox(
            height: 20,
          ),
          Row(
            children: rows,
          ),
          Row(children: <Widget>[
            Text("證件號碼",style: TextStyle(fontWeight: FontWeight.bold)),
            Expanded(child: TextField(onChanged: (str){
              this.id = str;
            },),),
          ],),
          SizedBox(
            height: 20,
          ),
          Row(children: <Widget>[
            Align(alignment: Alignment.centerLeft,child: MaterialButton(
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              height: 40,
              minWidth: 100,
              padding: EdgeInsets.all(0),
              color: Color(0xff4a701e),
              textColor: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius:
                  BorderRadius.all(Radius.circular(20))),
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text("上一步"),
            ),)
            ,Align(alignment: Alignment.centerLeft,child: MaterialButton(
              materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
              height: 40,
              minWidth: 100,
              padding: EdgeInsets.all(0),
              color: Color(0xff4a701e),
              textColor: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius:
                  BorderRadius.all(Radius.circular(20))),
              onPressed: () {
                var dic = this.widget.para;
                dic["id_no"] = this.id;
                dic["id_type"] = this.type+1;
                Navigator.push( context, MaterialPageRoute(builder: (context) => ReasonPage(title: widget.title,para: dic)),);
              },
              child: Text("下一步"),
            ),),
          ],),
        ],
      ),),
    );
  }
}